/* 自定义文本的国际化 */
import {createI18n} from 'vue-i18n';

// region 国际化文本
import enText from './valueText/enText.js'
import zhText from './valueText/zhText.js'
import zhTwText from './valueText/zhTwText.js'
// endregion



// region elemenu国际化
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import en from 'element-plus/dist/locale/en.mjs'
import zhTw from 'element-plus/dist/locale/zh-tw.mjs'

// element-plus国际化
export const var_i18n_element = () => {
  let result = {
    zh: zhCn,
    en: en,
    zhTw: zhTw
  }
  return result
}
// endregion


// 系统国际化文本
export const myLanguages = {
  // 英语
  en: enText,
  // 中文
  zh: zhText,
  // 中文繁体
  zhTw: zhTwText,
}


// 创建语言对象
export const var_i18n = createI18n({
  // legacy: true,
  locale: 'zh', // 设置默认语言
  fallbackLocale: 'zh', // 设置后备语言
  messages: myLanguages,
})


// 动态添加国际化文本。langObj：{ en: { bnText: { login: '' } } }
export function var_i18n_addLanguages(langObj = {}) {
// 找到en语言
  let thisLang = undefined
  let messages = var_i18n.global.messages
  for (let langKey in langObj) {
    thisLang = messages[langKey]
    if (thisLang == undefined) {
      // 这个语言不存在
      thisLang = langObj[langKey]
      messages[langKey] = thisLang
    } else {
      // 找第一层名称。bnText
      const firstObj = langObj[langKey]
      let thisFirst = undefined
      for (let key in firstObj) {
        thisFirst = thisLang[key]
        if (thisFirst == undefined) {
          // 找不到bnText等第一层
          thisFirst = firstObj[key]
          messages[langKey][key] = thisFirst
        } else {
          // 找到对应的字段
          const thisFieldObj = firstObj[key]
          for (let field in thisFieldObj) {
            messages[langKey][key][field] = thisFieldObj[field]
          }
        }
      }

    }
  }
}


// 解析文本
export function var_i18n_getText(text = '', data = undefined) {
  if (text == undefined || text == '') {
    return ''
  }
  let result = var_i18n.global.t(text, data)
  return result
}


// 表单/搜索的输入框提示：请输入XX
export const var_i18n_placeText = (text, formData, searchData) => {
  let textValue = var_i18n_getText(text)
  return var_i18n_getText("input.placeholder", {
    text: textValue
  })
}

// endregion




// region 国际化处理方法

// form表单国际化方法。item：itemList的数据。which：来源（label,select,selectPlaceholder,inputPlaceholder）
export const var_i18n_form = (item, which, formData) => {
  if (item.i18Text) {
    switch (which) {
      case 'label':
        return var_i18n_getText(item.i18Text)
        break;
      case 'inputPlaceholder':
        return var_i18n_placeText(item.i18Text)
        break;
    }
  }

  if (which == 'labelPopover') {
    return item?.popoverOptions?.content || ''
  }
  return item.label || item.placeholder
}

// 表格
export const var_i18n_table = (item, tableTitleItem = {}, which = 'content') => {
  if (which == 'content') {
    return item[tableTitleItem.prop]
  } else {
    // 表格字段
    if (tableTitleItem.i18Text) {
      return var_i18n_getText(tableTitleItem.i18Text)
    }
    return tableTitleItem.label
  }
}

// endregion







export default var_i18n;